<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class ForepartController extends AdminbaseController{
	

	public function index()
	{
		
		
	}
	//公众号列表
	public function Pubindex()
	{
		$this->_lists(3);
		$this->pubsubcomm();
		$this->display("pubindex");
	}
	public function Pubadd()
	{
		$this->_add(3);
		$this->pubsubcomm();
		$this->display("pubadd");
	}
	public function  PubList()
	{
		$this->_sort(3);
	}
	public function PubDel()
	{
		$this->_del(3);
	}
	public function PubEdit()
	{
		$this->_edit(3);
		$this->pubsubcomm();
		$this->display("pubedit");
	}
	
	public function pubsubcomm()
	{
		$comm=M("PubAssort")->order("listorder desc")->getField("id,t_name");
		$this->assign("comm",$comm);

	}
	protected function friendcomm()
	{
		$comm=M("circleType")->order("listorder desc")->getField("id,t_name");
		$this->assign("comm",$comm);
		
	}
	
	//订阅号

	public function Subindex()
	{
		$this->_lists(2);
		$this->pubsubcomm();
		$this->display("subindex");
	}
	public function Subadd()
	{
		$this->_add(2);
		$this->pubsubcomm();
		$this->display("subadd");
	}
	public function  SubList()
	{
		$this->_sort(2);
	}
	public function SubDel()
	{
		$this->_del(2);
	}
	public function SubEdit()
	{
		$this->_edit(2);
		$this->pubsubcomm();
		$this->display("subedit");
	}
	//朋友圈

	public function Friendindex()
	{
		$this->_lists(4);
		$this->friendcomm();
		$this->display("friendindex");
	}
	public function Friendadd()
	{
		$this->_add(4);
		$this->friendcomm();
		$this->display("friendadd");
	}
	public function  FriendList()
	{
		$this->_sort(4);
	}
	public function FriendDel()
	{
		$this->_del(4);
	}
	public function FriendEdit()
	{
		$this->_edit(4);
		$this->friendcomm();
		$this->display("friendedit");
	}

	
	
	public function ClientCaseIndex()
	{
		$this->_lists(1);
		$this->display("caseindex");
	}
	public function ClientCaseAdd()
	{
		$this->_add(1);	
		$this->display("caseadd");
	}
	
	public function ClientCaseList()
	{
		$this->_sort(1);
	}
	public function ClientCaseDel()
	{
		$this->_del(1);
	}
	public function ClientCaseEdit()
	{
		
		$this->_edit(1);
		$this->display("caseedit");
	}

	
	private function _edit($type=1)
	{
		$Fore=M("Foreport");
		if(IS_POST)
		{	
			$id=I("post.id");
			$where["id"]=$id;
			$where["pid"]=$type;
			$details=I("post.details");
			unset($_POST["id"],$_POST["details"]);
			$_POST["details"]=serialize($details);
			if($Fore->where($where)->save($_POST)){
				$this->success("更新成功");
			}else{
				$this->error("更新失败");
			}
		}
		$id=I("get.id");
		$where["pid"]=$type;
		$where["id"]=$id;
		$info=$Fore->where($where)->find();
		$info['details']=unserialize($info["details"]);
		$this->assign("id",$id);
		$this->assign("info",$info);
	}
	//删除
	private  function _del($type=1){
		$id=I("get.id");
		$Fore=M("Foreport");
		$where["id"]=$id;
		$where["pid"]=$type;
		if($Fore->where($where)->delete()){
			$this->success("删除成功");
		}else{
			$this->error("删除失败");
		}
	}
	private function _add($type=1)
	{
		
		if(IS_POST)
		{	
			$Fore=M("Foreport");
			$deta=I("post.details");
			$_POST["details"]=serialize($deta);
			$_POST["pid"]=$type;
			$_POST["uid"]=get_current_zhu_id();
			if($Fore->add($_POST)){
				$this->success("添加成功");
			}else{
				$this->error("添加失败");
			}
		}	
			
	}
	
	private  function _lists($type=1){
		$where["pid"]=$type;
		$Fore=M("Foreport");
		$count=$Fore->where($where)->count();
		$page = $this->page($count, 10);
		
		$data=$Fore->where($where)
		->limit($page->firstRow . ',' . $page->listRows)
		->order("list DESC")->select();
		foreach($data as $key=>$val)
		{
			if(!empty($val["details"]))
			{	
				$data[$key]['details']=unserialize($val["details"]);	
			}
		}
		$this->assign("data",$data);
		$this->assign("Page", $page->show('Admin'));
	}
	
	protected  function _sort($type){
		$where["pid"]=$type;
		$ids = $_POST['list'];
		$Fore=M("Foreport");
		foreach ($ids as $key => $r) {
			$data['list'] = $r;
			$where['id']=$key;
			$Fore->where($where)->save($data);
		}
		$this->success("更新成功");
		 
	}
	
	
	
	function updateImg(){
	
		$result=uploadImg();
		if($result["status"])
		{
			$this->ajaxReturn(sp_ajax_return(array("file"=>$result["file"]),"上传成功！",1),"AJAX_UPLOAD");
		}else{
			$this->ajaxReturn(sp_ajax_return(array(),$result["msg"],0),"AJAX_UPLOAD");
		}
	}
	
	
	
}